<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="事件状态" prop="eventStatus">
        <el-select
          filterable
          clearable
          v-model="queryParams.eventStatus"
          @change="handleQuery"
          style="width: 180px"
        >
          <el-option
            v-for="(item,index) in eventStatusOption"
            :key="index"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="主题" prop="eventTitle">
        <el-input
          v-model="queryParams.eventTitle"
          placeholder="请输入主题"
          clearable
          style="width: 180px"
          @change="handleQuery"
        />
      </el-form-item>
      <el-form-item label="事件等级" prop="eventLevel">
        <el-select
          filterable
          clearable
          v-model="queryParams.eventLevel"
          @change="handleQuery"
          style="width: 180px"
        >
          <el-option
            v-for="(item,index) in eventLevelOption"
            :key="index"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="watchMyself">
        <el-checkbox v-model="queryParams.watchMyself" @change="handleQuery" border>查看全部</el-checkbox>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-col>
    </el-row>

    <el-table
      ref="eventRef"
      id="eventId"
      v-loading="loading"
      :data="eventList"
      @selection-change="handleSelectionChange"
      :header-cell-style="tableHeadStyle"
      :row-class-name="tableRowClassName"
      height="680"
      border
    >
      <el-table-column type="selection" width="40" align="center" />
      <el-table-column
        label="事件ID"
        align="center"
        prop="id"
        width="70"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="事件状态"
        align="center"
        prop="eventStatus"
        width="80"
        :formatter="statusFormatter"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="事件等级"
        align="center"
        prop="eventLevel"
        width="100"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="主题"
        align="center"
        prop="eventTitle"
        width="210"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <a @click="watchHandler(scope.row)" :key="scope.row.id">{{ scope.row.eventTitle}}</a>
        </template>
      </el-table-column>
      <el-table-column
        label="内容"
        align="center"
        prop="eventContext"
        width="400"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="当前处理人"
        align="center"
        prop="nextNickName"
        width="100"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="创建人"
        align="center"
        prop="createUser"
        width="100"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="150"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime)}}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="更新人"
        align="center"
        prop="updateUser"
        width="100"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="更新时间"
        align="center"
        prop="updateTime"
        width="150"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="180">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="watchHandler(scope.row)">查看</el-button>
          <el-button
            v-if="editorButtonShow(scope.row)"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            v-if="editorButtonShow(scope.row)"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改事件记录看版对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="主题" prop="eventTitle">
          <el-input v-model="form.eventTitle" placeholder="请输入主题" style="width: 200px" />
        </el-form-item>
        <el-form-item label="内容" prop="eventContext">
          <el-input
            v-model="form.eventContext"
            placeholder="请输入内容"
            style="width: 280px"
            type="textarea"
          />
        </el-form-item>
        <el-form-item label="事件等级" prop="eventLevel">
          <el-select filterable v-model="form.eventLevel">
            <el-option
              v-for="(item,index) in eventLevelOption"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="操作类型" prop="operationType">
          <el-select filterable v-model="form.operationType">
            <el-option
              v-for="(item,index) in operationTypeOption"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>-->
        <el-form-item label="处理人" prop="processUserList">
          <el-select
            v-model="form.processUserList"
            filterable
            placeholder="请选处理人"
            style="width: 200px"
            multiple
            clearable
          >
            <el-option
              v-for="(item,index) in userOptions"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="处理意见" prop="disposeContext">
          <el-input
            v-model="form.disposeContext"
            placeholder="请输入内容"
            style="width: 280px"
            type="textarea"
          />
        </el-form-item>
        <el-form-item label="附件">
          <el-upload
            class="upload-demo"
            :headers="headers"
            :action="uploadUrl"
            :on-success="handleSuccess"
            :on-remove="handleRemove"
            :file-list="fileList"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--查看明细页 -->
    <!-- <WeekEventDetailIndex :windowOpen="windowOpen" :item="watchItem" @closeWindow="closeWindow"></WeekEventDetailIndex> -->
  </div>
</template>

<script>
import { listEvent, getEvent, delEvent, addEvent, updateEvent } from "@/api/file/weekEvent";
import WeekEventDetailIndex from '@/views/file/weekEventDetailIndex'
import { getSysUserList } from "@/api/share/project";
import { getToken } from "@/utils/auth";
export default {
  name: "Event",
  components: {
    WeekEventDetailIndex
  },
  data () {
    return {
      publicPath: process.env.BASE_URL,
      headers: { Authorization: "Bearer " + getToken() },
      uploadUrl: process.env.VUE_APP_BASE_API + "/storeFile/file/upload?type=weekEvent",
      fileList: [],
      uploadVisible: false,
      importLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      windowOpen: false,
      // 总条数
      total: 0,
      // 事件记录看版表格数据
      eventList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: "createTime",
        isAsc: "descending",
        watchMyself: false,
        eventStatus: null,
        eventTitle: null,
        eventContext: null,
        eventLevel: null,
        nextUser: null,
        createUser: null,
        updateUser: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        eventTitle: [
          { required: true, message: "主题不能为空", trigger: "blur" },
        ],
        eventContext: [
          { required: true, message: "内容不能为空", trigger: "blur" },
        ],
        eventLevel: [
          { required: true, message: "事件等级不能为空", trigger: "change" },
        ],
        nextUserName: [
          { required: true, message: "处理人不能为空", trigger: "change" },
        ],
        processUserList: [
          { required: true, message: "处理人不能为空", trigger: "change" },
        ]
      },
      eventLevelOption: [
        { "label": "重要", "value": "重要" },
        { "label": "一般", "value": "一般" },
        { "label": "低", "value": "低" },
      ],
      operationTypeOption: [
        { "label": "分配", "value": "分配" },
        { "label": "转办", "value": "转办" },
      ],
      eventStatusOption: [{ "label": "发布", "value": 0 },
      { "label": "办结", "value": 2 },],
      userOptions: [],
      watchItem: {},
      processUserList: [],
    };
  },
  created () {
    this.getUserOptions();
    this.getList();
  },
  methods: {
    /** 查询事件记录看版列表 */
    getList () {
      let self = this;
      this.loading = true;
      listEvent(this.queryParams).then(response => {
        this.eventList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        id: null,
        eventTitle: null,
        eventContext: null,
        eventLevel: null,
        nextUser: null,
        createUser: null,
        createTime: null,
        updateUser: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加事件";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      const id = row.id || this.ids
      getEvent(id).then(res => {
        this.form = res.data;
        this.open = true;
        this.title = "修改事件";
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateEvent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            // this.form.operationType = "发起";
            addEvent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除事件编号为"' + ids + '"的数据项？').then(function () {
        return delEvent(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download('weekEvent/event/export', {
        ...this.queryParams
      }, `事件记录看版_${new Date().getTime()}.xlsx`)
    },

    /*上传弹窗显示*/
    uploadDialog () {
      this.fileList = [];
      this.uploadVisible = true;
    },

    /*上传提交*/
    submitUpload () {
      this.$refs.uploadId.submit();
      this.uploadVisible = false;
      this.importLoading = true;
    },

    /*上传成功后操作*/
    handleSuccess (res, file, fileList) {
      let fileName = res.data.originalFilename;
      let fileId = res.data.fileId;
      this.form.fileId = fileId;
      this.form.fileName = fileName;

    },
    //删除上传的文件
    handleRemove (file, fileList) {
      if (fileList.length <= 0) {
        this.form.fileId = "";
        this.form.fileName = "";
        return;
      }
    },
    /**
     * 上传前处理
     */
    handleBeforeUpload (file) {
      let self = this;
      //15M
      const size = 1024 * 1024 * 15
      //校验文件大小
      if (file.size >= size) {
        self.$modal.msgError("文件大小超出15M，请重新选择！");
        self.uploadVisible = false;
        this.importLoading = false;
        return self.uploadVisible;
      }
      self.uploadVisible = true;
      return self.uploadVisible;
    },
    // 用户选择数据
    getUserOptions () {
      getSysUserList().then(response => {
        this.userOptions = [];
        if (response.data && response.data.length > 0) {
          this.userOptions = response.data;
        }
      });
    },
    /**
     * 打开查看弹窗
     */
    watchHandler (row) {
      let self = this;
      let url = "/weekMange/eventDetail";
      let { href } = self.$router.resolve({
        path: url,
        query: {
          "id": row.id,
        }
      });
      window.open(href, '_blank');
    },
    /**
     * 关闭弹窗
     */
    closeWindow () {
      let self = this;
      self.windowOpen = false;
    },

    /*状态处理*/
    statusFormatter (row, column, cellValue, index) {
      if (cellValue == 0) {
        return "发布";
      } else if (cellValue == 1) {
        return "处理中";
      } else if (cellValue == 2) {
        return "办结";
      } else {
        return "";
      }
    },
    /**
     * 编辑按钮处理
     */
    editorButtonShow (item) {
      let self = this;
      let nickName = self.$store.getters.userInfo.nickName;
      //办结
      if (item.eventStatus == 2) {
        return false;
      }
      if (item.createUser != nickName) {
        return false;
      }
      return true;

    }
  }
};
</script>
<style scoped>
a,
a:focus,
a:hover {
  cursor: pointer;
  color: blue;
  text-decoration: none;
}
</style>
